package com.ecity.ui {
	import com.ecity.events.MenuEvent;
	import com.ecity.resource.Resource;
	import com.greensock.data.TweenLiteVars;
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Yicheng.Zhang
	 */
	public class CategoryMenu extends Sprite{
		private var titleBed:Sprite;
		private var menuBed:Sprite;
		
		public var isOpend:Boolean;
		
		private var topHeight:int;
		public var count:int;
		public var index:int;
		private var states:Array = new Array();
		
		public function CategoryMenu(index:int, isOpen:Boolean = false) {
			this.index = index;
			var bitmap:Bitmap = Resource.categroys.get("bt_"+index) as Bitmap;
			menuBed = new Sprite();
			titleBed = new Sprite();
			this.addChild(menuBed);
			titleBed.addChild(bitmap);
			this.addChild(titleBed);
			titleBed.addEventListener(MouseEvent.CLICK, onCategroyClicked);
			topHeight = bitmap.height;
			this.isOpend = isOpen;
			this.y = index * 50;
		}
		
		private function onCategroyClicked(e:MouseEvent):void {
			if (isOpend) return;
			expand();
			this.dispatchEvent(new MenuEvent(MenuEvent.CATE_CHANGED));
		}
		
		public function addSubMemu(count:int):void {
			this.count = count;
			for (var i:int = 0; i < count; i++) {
				var submenu:SubMenu = new SubMenu(i);
				if (isOpend) {
					submenu.y = i * 38 + 50;
				}else {
					submenu.y = 0;
				}
				submenu.alpha = isOpend?1:0;
				submenu.addEventListener(MenuEvent.MENU_CHANGED, onMenuChangedHandler);
				menuBed.addChild(submenu);
			}
		}
		
		
		private function onMenuChangedHandler(e:MenuEvent):void {
			var menu:SubMenu = e.target as SubMenu;
			this.dispatchEvent(new MenuEvent(MenuEvent.CATE_MENU_CHANGED,menu.numMenu * 25));
			updateMenus(menu.index);
		}
		/**
		 * 子菜单管理
		 * @param	selectedIndex
		 */
		private function updateMenus(selectedIndex:int):void {
			var beSelected:SubMenu = menuBed.getChildAt(selectedIndex) as SubMenu;
			TweenLite.to(beSelected, 0.2 ,{y:selectedIndex* 38 + topHeight,delay:0.5} );
			for (var i:int = 0; i < count; i++) {
				var subMenu:SubMenu = menuBed.getChildAt(i) as SubMenu;
				if (subMenu.index != selectedIndex && subMenu.isSelected) {
					subMenu.pickup();
				}
				if (subMenu.index > selectedIndex) {
					TweenLite.to(subMenu, 0.2 ,{y:(i+1)* 38+(beSelected.numMenu-1)*25+topHeight,delay:1} );
				}else if(subMenu.index < selectedIndex) {
					TweenLite.to(subMenu, 0.2 , { y:i * 38 + topHeight, delay:0.5} );
				}
			}
		}

		/**
		 * 展开菜单&移位
		 */
		protected function expand():void {
			if (this.isOpend) return;
			isOpend = true;
			for (var i:int = 0; i < menuBed.numChildren; i++) {
				var menu:SubMenu = menuBed.getChildAt(i) as SubMenu;
				TweenLite.to(menu, 0.5, {delay:(menuBed.numChildren-menu.index)* 0.2+0.5, y:50+i*38,alpha:1});
			}
		}
		
		/**
		 * 收起菜单&移位
		 */
		public function pickup(stepdelay:Number=0.05):void {
			if (!this.isOpend) return;
			isOpend = false;
			for (var i:int = 0; i < menuBed.numChildren; i++) {
				var menu:SubMenu = menuBed.getChildAt(i) as SubMenu;
				menu.pickup();
				TweenLite.to(menu, 0.5, { delay:menu.index * stepdelay, y:0,alpha:0} );
			}
		}
		
	}

}